import com.mapper.WorkerMapper;
import com.pojo.Worker;
import com.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

public class WorkerTest {
    @Test
    public void findWorkerByIdTest() {
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);

        Worker worker = mapper.selectWorker(1);
        System.out.println(worker);
        session.close();
    }
    @Test
    public void insertWorkerTest() {
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        Worker worker = new Worker();
        worker.setName("赵七");
        worker.setAge(36);
        worker.setSex("女");
        worker.setWorkerId(10004);
        int result = mapper.insertWorker(worker);
        if (result>0){
            System.out.println("插入成功");
        }else {
            System.out.println("插入失败");
        }
        System.out.println(worker);
        session.commit();
        session.close();
    }
    @Test
    public void updateWorkerTest() {
        SqlSession session = MyBatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(4);
        worker.setName("李华");
        worker.setAge(28);
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        int result = mapper.updateWorker(worker);
        if (result>0){
            System.out.println("更新成功"+result+"条数据");
        }else {
            System.out.println("更新失败");
        }
        System.out.println(worker.toString());
        session.commit();
        session.close();
    }
    @Test
    public void deleteWorkerTest() {
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        int result = mapper.deleteWorker(4);
        if (result>0){
            System.out.println("删除成功"+result+"条数据");
        }else {
            System.out.println("删除失败");
        }
        session.commit();
        session.close();
    }
    @Test
    public void selectWorkerByIdAndNameTest() {
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        Worker worker = mapper.selectWorkerByIdAndName(3, "王五");
        System.out.println(worker.toString());
        session.close();
    }
}
